/**
 * @author ZeroLee
 * @date 2007-12-21
 */

/*************************************
 * for action of image...
 *************************************/
function G(id) {
	return document.getElementById(id);
}
var TheImage = {
	MediumWidth:100,
	HeadWidth:200
}

/**
 * validation of image ext....
 * if there is any ext for validing, please add to the var of type
 * @param {Object} fileUrl
 */
TheImage.checkImageType = function(fileUrl) {
	//alert(fileUrl);
	var type = new Array(".jpg", ".jpeg", ".png", ".bmp");
	var typeLength = type.length;
	
	var imgUrl = fileUrl.toLowerCase();
	var postfixLen = imgUrl.length;
	var len4 = imgUrl.substring(postfixLen - 4, postfixLen);
	var len5 = imgUrl.substring(postfixLen - 5, postfixLen);
	for(var i = 0; i < typeLength; i++) {
		if(len4 == type[i] || len5 == type[i]) {
			return true;
		}
	}
	return false;
}

TheImage.preLoadImage = function(id, fileUrl) {
	var image = new Image();
	//for firefox this is no file:/// to add
	if((fileUrl.indexOf("file:///") == -1) && (fileUrl.indexOf("http://") == -1)) {
		fileUrl = "file:///" + fileUrl;
		fileUrl = fileUrl.replace(/\\/g,"/");
	}
	//alert(fileUrl);
	image.src = fileUrl;
	//if(!window.all) {
	//	alert('aa');
	//	image.width = 100;
	//	image.height = 200;
	//}
	var height = TheImage.getHeightByWidth(image, TheImage.HeadWidth);
	image.width = TheImage.HeadWidth;
	image.height = height;
	var elem = G(id);
	var innerHtml = "<img src=\"" + image.src + "\" style=\"width:" + image.width + "px;height=\"" + image.height + "px;\"/>";
	//alert(innerHtml);
	elem.innerHTML = innerHtml;
}

/**
 * 
 * @param {Object} o a image object
 * @param {Object} width the needed width for the image
 */
TheImage.getHeightByWidth = function(image, width) {
	//alert(image.width);
	var temp = width / image.width;
	//alert(temp);
	return image.height * temp;
}

/**
 * delete the image of id innerHTML
 * @param {Object} id
 */
TheImage.delImg = function(id) {
	var defaultImg = "<img src=\"resource/default.jpg\"/>";
	var elem = G(id);
	elem.innerHTML = defaultImg;
}
